//
//  DBDataFactory.m
//  CslpodDictionary
//
//  Created by gtts on 13-4-18.
//  Copyright (c) 2013年 cslpod. All rights reserved.
//

#import "DBDataFactory.h"
#import "SandboxFile.h"

@interface DBDataFactory ()

@property(retain,nonatomic)id classValues;

@end


static FMDatabaseQueue* queue;

@implementation DBDataFactory
@synthesize classValues;
@synthesize databasePath;

+ (DBDataFactory *)sharedDBDataFactory
{
    static id ShardInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ShardInstance=[[self alloc]init];
    });
    return ShardInstance;
}

- (id)init
{
    if (self= [super init]) {
        self.databasePath = nil;
    }
    return self;
}

- (void)setDatabasePath:(NSString *)aDatabasePath
{
    if (aDatabasePath == nil) {
        databasePath = nil;
    }else{
        databasePath = [SandboxFile GetPathForDocuments:aDatabasePath];
    }
}

- (NSString *)databasePath
{
     NSAssert(databasePath != nil, @"database path is null!");
    return databasePath;
}

- (BOOL)isExistDB
{
    BOOL result = NO;
    if ([SandboxFile IsFileExists:self.databasePath])
    {
        result = YES;
    }
    return result;
}

- (void)createDB
{
    if (![self isExistDB]) {
       queue=[[FMDatabaseQueue alloc]initWithPath:self.databasePath];
    }
}

- (void)createTable:(Class)modelClass
{
    [self createDB];
    id modle = [[modelClass alloc] initWithDBQueue:queue];
    if (modle) {
        NSLog(@"新建表成功！！");
    }else{
        NSLog(@"新建表失败！！");
    }
}

- (id)Factory:(Class)modelClass
{
    id result;
    queue = [[FMDatabaseQueue alloc]initWithPath:self.databasePath];
    result=[[modelClass alloc] initWithDBQueue:queue];
    return result;
}


- (void)insertIntoDB:(id)model classType:(Class)modelClass
{
    self.classValues=[self Factory:modelClass];
    [classValues insertToDB:model callback:^(BOOL Values)
     {
         NSLog(@"添加%d",Values);
     }];
}

- (void)updateData:(id)model classType:(Class)modelClass
{
    self.classValues=[self Factory:modelClass];
    [classValues updateToDB:model callback:^(BOOL Values)
     {
         NSLog(@"修改%d",Values);
     }];
}

- (void)deleteData:(id)model classType:(Class)modelClass
{
    self.classValues=[self Factory:modelClass];
    [classValues deleteToDB:model callback:^(BOOL Values)
     {
         NSLog(@"删除%d",Values);
     }];
}

- (void)clearTableData:(Class)modelClass
{
    self.classValues=[self Factory:modelClass];
    [classValues clearTableData];
    NSLog(@"删除全部数据");
}

- (void)deleteWhereData:(NSDictionary *)model classType:(Class)modelClass
{
    self.classValues=[self Factory:modelClass];
    [classValues deleteToDBWithWhereDic:model callback:^(BOOL Values)
     {
         NSLog(@"删除成功");
     }];
}

- (void)searchWhere:(NSDictionary *)where orderBy:(NSString *)columeName offset:(int)offset count:(int)count classType:(Class)modelClass callback:(void(^)(NSArray *))result
{
    self.classValues=[self Factory:modelClass];
    [classValues searchWhereDic:where orderBy:columeName offset:offset count:count callback:^(NSArray *array)
     {
         result(array);
     }];
}

@end
